package com.store.practice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageInfo;
import com.store.practice.common.api.CommonPage;
import com.store.practice.mapper.BgAppointmentOrderMapper;
import com.store.practice.model.BgAppointmentOrder;
import com.store.practice.model.BgAppointmentOrderExample;
import com.store.practice.service.BgOrderCaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BgOrderCaseServiceImpl implements BgOrderCaseService {
    @Autowired
    private BgAppointmentOrderMapper orderMapper;

    @Override
    public List<Map> listStoreOrder(Integer storeId, Date date) {
        BgAppointmentOrderExample example = new BgAppointmentOrderExample();
        example.setOrderByClause("end_time desc");
        BgAppointmentOrderExample.Criteria criteria = example.createCriteria();
        criteria.andStoreIdEqualTo(storeId);
        if (date != null){
            criteria.andServiceDateBetween(DateUtil.beginOfDay(date),DateUtil.endOfDay(date));
        }
        List<BgAppointmentOrder> appointmentOrders = orderMapper.selectByExample(example);
        List<Map> result = new ArrayList<>();
        if (appointmentOrders!=null){
            for (BgAppointmentOrder order:appointmentOrders){
                Map<String, Object> map = new HashMap<>();
                map = BeanUtil.beanToMap(order);
                map.put("serviceDate", DateUtil.format(order.getServiceDate(),"yyyy-MM-dd"));
                map.put("startTime",DateUtil.format(order.getStartTime(),"HH:mm"));
                map.put("endTime",DateUtil.format(order.getEndTime(),"HH:mm"));
                switch (order.getStatus()){
                    case 0:
                        map.put("status","已取消");
                        break;
                    case 1:
                        map.put("status","预约中");
                        break;
                    case 2:
                        map.put("status","已到店");
                        break;
                }
                result.add(map);
            }
        }
        return result;
    }

    @Override
    public List<Map> listMemBerOrder(Integer mid, Date date) {
        BgAppointmentOrderExample example = new BgAppointmentOrderExample();
        example.setOrderByClause("end_time asc");
        BgAppointmentOrderExample.Criteria criteria = example.createCriteria();
        criteria.andMbrIdEqualTo(mid);
        if (date != null){
            criteria.andServiceDateBetween(DateUtil.beginOfDay(date),DateUtil.endOfDay(date));
        }
        List<BgAppointmentOrder> appointmentOrders = orderMapper.selectByExample(example);
        List<Map> result = new ArrayList<>();
        if (appointmentOrders!=null){
            for (BgAppointmentOrder order:appointmentOrders){
                Map<String, Object> map = new HashMap<>();
                map = BeanUtil.beanToMap(order);
                map.put("serviceDate", DateUtil.format(order.getServiceDate(),"yyyy-MM-dd"));
                map.put("startTime",DateUtil.format(order.getStartTime(),"HH:mm"));
                map.put("endTime",DateUtil.format(order.getEndTime(),"HH:mm"));
                switch (order.getStatus()){
                    case 0:
                        map.put("status","已取消");
                        break;
                    case 1:
                        map.put("status","预约中");
                        break;
                    case 2:
                        map.put("status","已到店");
                        break;
                }
                result.add(map);
            }
        }
        return result;
    }
}
